//
//  AddressfriendTableViewController.m
//  Weiweiyunshi
//
//  Created by 张旭峰 on 2017/2/14.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "AddressfriendTableViewController.h"
//#import "AddressPhoneNumber.h"
#import "AddressPhoneNumber.h"
#import "LoginModel.h"
#import "FriendListModel.h"
#import "AddressfriendTableViewCell.h"
#import "ResultViewController.h"
#import "PeopleModel.h"
#import "OrginformationController.h"
@interface AddressfriendTableViewController ()
{
    NSMutableArray * modelArray;
    NSMutableArray *modelArray1;
}
@property(nonatomic,copy)NSMutableArray * Array;
@property(nonatomic,copy)NSMutableArray * friendArray;
//@property(nonatomic,copy)NSMutableArray * modelArray;

@end

@implementation AddressfriendTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.navigationItem.title = @"通讯录朋友";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_back_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = backItem;
     modelArray = [NSMutableArray array];
    modelArray1 = [NSMutableArray array];
     modelArray1 =  [PeopleModel sharedSingleton];

     [self setupUI];
    [self loadData];
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight=200;

     [self.tableView registerNib:[UINib nibWithNibName:@"AddressfriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}
-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)setupUI
{
    UITableView *tableView= [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    [self.view addSubview: tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource = self;
    
    self.tableView = tableView;

}
-(void)loadData
{
    
//    AddressPhoneNumber * Nub = [[AddressPhoneNumber alloc] init];
//    NSArray * array =
   AddressPhoneNumber *VC =[[AddressPhoneNumber alloc]init];
    
    self.Array =[VC getSetArr];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userId =  [userDefaults objectForKey:@"loginName"];
    NSString *url = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/contact.do?userId=%@&pageNum=1&count=200",userId];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [NSArray yy_modelArrayWithClass:[FriendListModel class] json:responseObject[@"contact"]];
        _friendArray = array;
         [self.tableView reloadData];
//        NSArray *arr = dic[@"contact"];
//        self.collectionArray = array;
//        
////        _nameSource =[[NSMutableArray alloc]init];
//        
//        for (int i=0; i<arr.count; i++) {
//            NSDictionary *dic1 = arr[i];
//            FriendListModel *model = [FriendListModel yy_modelWithDictionary:dic1];
//            [_nameSource addObject:model.name];
//            //            [dataSource addObject:model];
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
    
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.Array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressfriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NSString * phonemumber = self.Array[indexPath.row];
    
//     cell.phonemumberr.text = phonemumber;
    BOOL isfriend=NO;

    for (FriendListModel * model12 in  _friendArray) {
        
        if([model12.mobile isEqualToString: phonemumber])
        {
            
            
            isfriend = YES;
            
            cell.phonemumberr.text = model12.name;
            [modelArray addObject:model12];
            break;
        }else{
            
            for (PeopleModel * model in modelArray1) {
                if([ model.mobile isEqualToString:phonemumber])
                {
                    cell.phonemumberr.text = model.userName;
                    break;
                }
            }
        }
        
    }
    
    if(isfriend) {
        
       
        
       
        [cell.isButton setTitle:@"已添加" forState: UIControlStateNormal ];
//        [cell.isButton.titleLabel setTintColor:[UIColor colorWithRed:163 green:163 blue:163]];
        [cell.isButton setTitleColor:[UIColor colorWithRed:163 green:163 blue:163] forState:UIControlStateNormal];
         
//        cell.isButton.backgroundColor=[UIColor whiteColor];
        
        [cell.isButton setBackgroundColor:[UIColor whiteColor]];
        cell.isButton.enabled = false;
        return cell;

        
    }else{
        
      
        [cell.isButton setTitle:@"添加" forState: UIControlStateNormal ];
        
//        [cell.isButton.titleLabel setTintColor:[UIColor colorWithRed:255 green:255 blue:255]];
        
//        [cell.isButton setTitleColor:[UIColor colorWithRed:255 green:255blue:255] forState:UIControlStateNormal];
        [cell.isButton setTitleColor:[UIColor colorWithRed:255 green:255 blue:255] forState:UIControlStateNormal];
        
        [cell.isButton setBackgroundColor:[UIColor colorWithRed:32 green:203 blue:96]];
        cell.isButton.enabled = false;
        return cell;

    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.001;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    AddressfriendTableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if([cell.isButton.titleLabel.text isEqualToString:@"添加"])
    {
//        FriendListModel * MOdel = modelArray[indexPath.row];
//        
//        NSString *userid = MOdel.idd;
        
        ResultViewController * VC = [[ResultViewController alloc]init];

//        modelArray1 =  [PeopleModel sharedSingleton];
        NSString *str = cell.phonemumberr.text;
        for (PeopleModel * model12 in  modelArray1) {
            
            if([model12.userName isEqualToString: str])
            {
                
                VC.userId = model12.userId;
                break;
            }
        }
        //        VC.userId = userid;
        [self.navigationController pushViewController:VC animated:NO];
        
    }else{
        
        
        OrginformationController *orinforVC = [[OrginformationController alloc]init];
//        NSDictionary *dict = _nameIndex[indexPath.section];
//        NSMutableArray *array = dict[@"content"];
        NSString *str = cell.phonemumberr.text;
        for (FriendListModel * model123 in _friendArray) {
            if([model123.name isEqualToString:str])
            {
//          FriendListModel * model123= modelArray[indexPath.row];
                orinforVC.userName =model123.name;
                orinforVC.deptName =model123.company;
                
                orinforVC.pdeptName =model123.dept;
                orinforVC.email =model123.email;
                orinforVC.mobile =model123.mobile;
                orinforVC.img =model123.photo;
                
                orinforVC.vidyoKey = model123.vidyoKey;
                orinforVC.vidyoURL = model123.vidyourl;
                orinforVC.domainName = model123.domainName;
                break;
            }
        }
        [self.navigationController pushViewController: orinforVC animated:NO];
        
    }
    
    
    
}
@end
